import com.mit.web.business.proxy.MaintenanceProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.Constants;
import com.mit.web.config.SizeGroupConfig;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.util.FlexUtil;
import com.mit.web.util.LogUtil;
import com.mit.web.util.MessageUtil;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.view.design.StyleDetailPanelD;
import com.mit.web.vo.maintenance.EmployeeVO;

import flash.events.MouseEvent;
import flash.geom.Point;

import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.logging.ILogger;
import mx.resources.ResourceManager;
import mx.rpc.events.ResultEvent;
import mx.utils.StringUtil;

private var _empId:int;
private var _logger:ILogger;
[Bindable]private var _empVo:EmployeeVO = new EmployeeVO();
private var _proxy:MaintenanceProxy = MaintenanceProxy.getInstance();
private var _pm:IProgressMonitor;


public function set empId(id:int):void{
	_empId = id;
}


private function init():void{
	_logger = LogUtil.getLogger(this);
	
	if (_empId > 0){
		//Retrieve Employee
		_logger.debug("retrieve existing employee");
		var responder:IInvokeResponder = _proxy.retrieveEmployeeById(_empId);
		responder.addResultListener(loadEmployeeHandler);
		responder.addFaultListener(exceptionHandler);
	}else{
		_logger.debug("new employee");
//		bundleSources();
	}		
}

private function loadEmployeeHandler(event:ResultEvent):void{
	_empVo = event.result as EmployeeVO;
//	bundleSources();
}

/**
 * Bundle data for Panel
 * */
//private function bundleSources():void{
//	employeePanel.employee = _empVo;
//}

private function doSave(event:Event):void{
	// validate data
	if(!validate())
		return;
	
	assembleVO();
	
	if(mx.validators.Validator.validateAll(validators).length == 0 ){
		// Call Save with progress bar
		var responder:IInvokeResponder;
		if (_empId > 0) {
			responder = _proxy.updateEmployee(_empVo).installProgressBar(_pm, this);
		} else {
			responder = _proxy.createEmployee(_empVo).installProgressBar(_pm, this);
		}
		responder.addFaultListener(exceptionHandler);
		responder.addResultListener(savedHandler);
	}else{
		// can't save
		event.preventDefault();
	}
}

private function savedHandler(event:ResultEvent):void{
	var e:PopWinCloseEvent;
	if(_empVo.id > 0){
		e = new PopWinCloseEvent(PopWinCloseEvent.SAVED);
	}else{
		e = new PopWinCloseEvent(PopWinCloseEvent.CREATED);
	}
	_empVo = event.result as EmployeeVO;	
	e.msg = ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"info.common.saved",[_empVo.username]);
	e.vo = _empVo;
	this.dispatchEvent(e);
	closeWindow(this);	
}

// assemble fields to VO
private function assembleVO():void{
	_empVo.name = txt_name.text;
	_empVo.mobile = txt_mobile.text;
	_empVo.email = txt_email.text;
	_empVo.username = txt_username.text;
	_empVo.profileId = parseInt(txt_profileId.text);
	_empVo.password = txt_password.text;
	// !!!!!!!! MUST BE REVISED!!!!
	_empVo.orgId = 1;
	_empVo.adminInd = 'N';
	//_empVo.designerId = employeePanel.bx_status.text;
	_logger.debug("name=" + _empVo.name + ", mobile=" + _empVo.mobile 
		+ ", email=" + _empVo.email + ", username=" + _empVo.username
		+ ", profileId=" + _empVo.profileId + ", status=" + _empVo.activeInd
		+ ", password=" + _empVo.password);
}

private function validate():Boolean{
	var rtn:Boolean = true;
	if(com.mit.web.util.Validator.isNullOrBlank(txt_name.text)){
		showRequiredErrorTip(txt_name);
		rtn = false;
	}
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_mobile.text)){
		showRequiredErrorTip(txt_mobile);
		rtn = false;
	}
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_email.text)){
		showRequiredErrorTip(txt_email);
		rtn = false;
	}
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_username.text)){
		showRequiredErrorTip(txt_username);
		rtn = false;
	}
//	if(rtn && Validator.isNullOrBlank(employeePanel.txt_profileId.text)){
//		showRequiredErrorTip(employeePanel.txt_profileId);
//		rtn = false;
//	}
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(_empVo.activeInd)){
		showRequiredErrorTip(bx_status);
		rtn = false;
	}
	return rtn;
}

private function doDelete():void{
	var responder:IInvokeResponder = _proxy.deleteEmployee(_empVo).installProgressBar(_pm, this);
	responder.addFaultListener(exceptionHandler);
	responder.addResultListener(deletedHandler);
}

private function deletedHandler(event:ResultEvent):void{
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.DELETED);
	e.msg = ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"info.common.deleted",[_empVo.username]);
	this.dispatchEvent(e);
	closeWindow(this);	
}